package com.ilia.leek.fund.xiong;

import com.dtflys.forest.http.ForestResponse;
import com.ilia.leek.common.enums.ResultCode;
import com.ilia.leek.common.exception.BaseBusinessException;
import com.ilia.leek.fund.FundConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Alice on 2021/4/26
 * @version 1.0
 * @since 1.0
 */
@Component
@Slf4j
public class HttpDoctorXiongFund {

    @Resource
    private DoctorXiongHttpClient doctorXiongHttpClient;

    /**
     * 查询所有基金
     *
     * @return {@link DoctorXiongResponse}
     */
    public DoctorXiongResponse getAllFund() {
        log.info("http get from DoctorXiong,getAllFund");
        ForestResponse<DoctorXiongResponse> response = doctorXiongHttpClient.getAllFund(FundConstant.DOCTORXIONG_ALL_FUND);
        log.info("getAllFund response:{}", response);
        if (response.getStatusCode()==200) {
            return response.getResult();
        } else {
            throw new BaseBusinessException(ResultCode.NET_ERROR);
        }
    }

    /**
     * 查询小熊的基金详情
     * e:https://api.doctorxiong.club/v1/fund/detail?code=000001&startDate=2020-09-01
     *
     * @return {@link DoctorXiongResponse}
     */
    public DoctorXiongResponse getDetail(String code) {
        log.info("http get from DoctorXiong,fund code:{}", code);
        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        ForestResponse<DoctorXiongResponse> response = doctorXiongHttpClient.getDetail(FundConstant.DOCTORXIONG_DETAIL + "?code=" + code + "&startDate=" + date);
        log.info("getDetail response:{}", response);
        if (response.getStatusCode() == 200) {
            return response.getResult();
        } else {
            throw new BaseBusinessException(ResultCode.NET_ERROR);
        }
    }

    /**
     * 查询小熊的基金详情
     * e:https://api.doctorxiong.club/v1/fund/detail?code=000001&startDate=2020-09-01
     *
     * @return {@link DoctorXiongResponse}
     */
    public DoctorXiongResponse getFund(String codes) {
        log.info("http get from DoctorXiong,fund list:{}", codes);
        ForestResponse<DoctorXiongResponse> response = doctorXiongHttpClient.getFund(FundConstant.DOCTORXIONG_FUND + "?code=" + codes);
        log.info("getFund response:{}", response);
        if (response.getStatusCode() == 200) {
            return response.getResult();
        } else {
            throw new BaseBusinessException(ResultCode.NET_ERROR);
        }
    }
}
